package com.lxk.service.impl;

import com.lxk.dao.*;
import com.lxk.pojo.*;
import com.lxk.service.OrdersService;
import com.lxk.util.exception.GlobalException;
import com.lxk.vo.OrdersDetailModel;
import com.lxk.vo.OrdersInfoModel;
import com.lxk.vo.PageModel;
import com.lxk.vo.PointOrdersModel;
import com.lxk.vo.param.OrdersParam;
import com.lxk.vo.result.CodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/3/30 16:51
 * @description
 */
@Service
@Transactional
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private AsMyMapper mm;
    @Autowired
    private OrdersMapper om;
    @Autowired
    private DeliveryMapper dm;
    @Autowired
    private PointOrdersMapper pom;
    @Autowired
    private GoodsComMapper gcm;


    public Integer getTotalPages(Integer num, OrdersParam ordersParam) {
        Integer count = mm.countOrders2(ordersParam);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<OrdersInfoModel> selOrdersPage(Integer pageIndex, Integer num, Integer totalPages, OrdersParam ordersParam) {
        int start = (pageIndex-1)*num;
        List<OrdersInfoModel> list = mm.selOrdersPage2(start, num, ordersParam);
        PageModel<OrdersInfoModel> model = new PageModel<OrdersInfoModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Orders selOrdersById(Integer ordersId) {
        Orders orders = om.selectByPrimaryKey(ordersId);
        return orders;
    }

    public Orders selOrdersByOutTradeNo(String outTradeNo) {
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.createCriteria();
        criteria.andOutTradeNoEqualTo(outTradeNo);
        List<Orders> list = om.selectByExample(example);
        if (list.size()>0) {
            return list.get(0);
        }else{
            return null;
        }
    }

    public Integer updOrders(Orders orders) {
        int upd = om.updateByPrimaryKeySelective(orders);
        return upd;
    }

    public Integer getUnsendTotalPages(Integer num) {
        Integer count = mm.countOrders(1);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<OrdersInfoModel> selUnsendOrdersPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<OrdersInfoModel> list = mm.selOrdersPage(start, num, sort, order, 1);
        PageModel<OrdersInfoModel> model = new PageModel<OrdersInfoModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Delivery selDeliveryById(Integer deliveryId) {
        Delivery delivery = dm.selectByPrimaryKey(deliveryId);
        return delivery;
    }

    public Delivery selDeliveryFromPointOrders(Integer pointOrdersId) {
        PointOrders orders = pom.selectByPrimaryKey(pointOrdersId);
        if (orders==null) {
            throw new GlobalException(CodeMsg.ORDER_NOT_EXIST);
        }
        Delivery delivery = new Delivery();
        delivery.setDeliveryAddr(orders.getDeliveryAddr());
        delivery.setDeliveryCity(orders.getDeliveryCity());
        delivery.setDeliveryId(orders.getDeliveryId());
        delivery.setDeliveryName(orders.getDeliveryName());
        delivery.setDeliveryPhone(orders.getDeliveryPhone());
        delivery.setWxUserId(orders.getWxUserId());
        return delivery;
    }

    public void setExpress(Integer ordersId, String expressNo) {
        Orders orders = new Orders();
        orders.setOrdersId(ordersId);
        orders.setExpressNo(expressNo);
        orders.setOrdersStatus(2);
        om.updateByPrimaryKeySelective(orders);
    }

    public OrdersDetailModel queryOrders(Integer ordersId) {
        OrdersDetailModel ordersDetailModel = mm.selOrdersDetailById(ordersId);
        return ordersDetailModel;
    }

    public Integer getPointOrderTotalPages(Integer num) {
        PointOrdersExample example = new PointOrdersExample();
        PointOrdersExample.Criteria criteria = example.createCriteria();
        criteria.andIsSuccessEqualTo(1);
        criteria.andExpressNoIsNull();
        Integer count = (int)pom.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<PointOrdersModel> selPointOrdersPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<PointOrdersModel> list = mm.selPointOrdersPage(start, num, sort, order);
        PageModel<PointOrdersModel> model = new PageModel<PointOrdersModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public void setPointOrdersExpress(Integer pointOrdersId, String expressNo) {
        PointOrders orders = new PointOrders();
        orders.setPointOrdersId(pointOrdersId);
        orders.setExpressNo(expressNo);
        pom.updateByPrimaryKeySelective(orders);
    }

    public GoodsCom getGoodsComByOrdersId(Integer ordersId) {
        GoodsComExample example = new GoodsComExample();
        GoodsComExample.Criteria criteria = example.createCriteria();
        criteria.andOrdersIdEqualTo(ordersId);
        List<GoodsCom> goodsComs = gcm.selectByExample(example);
        if (goodsComs.size() == 0) {
            return null;
        }else{
            return goodsComs.get(0);
        }
    }

    public void delGoodsCom(Integer goodsComId) {
        gcm.deleteByPrimaryKey(goodsComId);
    }

    public BigDecimal getGoodsSales(Integer goodsId, Date begin, Date end) {
        BigDecimal goodsSales = mm.getGoodsSales(goodsId, begin, end);
        if (goodsSales == null) {
            return new BigDecimal(0);
        }
        return goodsSales;
    }

    public BigDecimal getShopSales(Integer shopId, Date begin, Date end) {
        BigDecimal shopSales = mm.getShopSales(shopId, begin, end);
        if (shopSales == null) {
            return new BigDecimal(0);
        }
        return shopSales;
    }

    public Date getBeginTime(Integer type) {
        Calendar calendar = Calendar.getInstance();
        switch (type) {
            case 1:
                calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
                return calendar.getTime();
            case 2:
                calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
                return calendar.getTime();
            case 3:
                calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                return calendar.getTime();
            case 4:
                calendar.set(Calendar.MILLISECOND,0);
                calendar.set(Calendar.SECOND,0);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.HOUR_OF_DAY,0);
                return calendar.getTime();
        }
        calendar.setTime(new Date());
        return calendar.getTime();
    }


}
